﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Dycox.Validation;

namespace Dycox.Workflows.Conditions
{
    public class RegexCondition : PropertyValueCondition
    {
        public RegexCondition(Dictionary<string, string> parameters)
            : base(parameters)
        {
            string value;

            if (parameters.TryGetValue("pattern", out value))
                _pattern = value;
            else
                throw new InvalidOperationException("Regex pattern not set.");

            if (parameters.TryGetValue("ignoreCase", out value))
            {
                bool ignoreCase = bool.Parse(value);
                if (!ignoreCase) _options = RegexOptions.IgnoreCase;
            }
        }

        string _pattern;

        RegexOptions _options;

        protected override Validation.Validator CreateInnerValidator()
        {
            RegexValidator rv = new RegexValidator();
            rv.Pattern = _pattern;
            rv.Options = _options;
            return rv;
        }
    }
}
